package net.toocruel.yarn.mapreduce.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.StringTokenizer;

/**
 * @author : 宋同煜
 * @version : 1.0
 * @createTime : 2017/4/12 14:15
 * @description :
 */
public class WordCountMapper extends Mapper<Object,Text,Text,IntWritable>{

    //对于每个单词赋予出现频数1，因为单词是一个一个取出来的，所以每个数量都为1
    private final static IntWritable one = new IntWritable(1);
    //存储取出来的一行单词
    private Text word = new Text();

    @Override
    protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
        //StringTokenizer 对输入单词进行切分
        StringTokenizer itr = new StringTokenizer(value.toString());
        while(itr.hasMoreTokens())
        {
            word.set(itr.nextToken());
            context.write(word, one);
        }
    }
}
